﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace MidimediaFinder
{
    public partial class ResultsPage : PhoneApplicationPage
    {
        // Constructor
        public ResultsPage()
        {
            InitializeComponent();

            // Set the data context of the listbox control to the sample data
            DataContext = App.ViewModel;
            Items_List.DataContext = App.ViewModel;
            Promotions_List.DataContext = App.ViewModel;
            this.Loaded += new RoutedEventHandler(ResultsPage_Loaded);
        }

        // Load data for the ViewModel Items
        private void ResultsPage_Loaded(object sender, RoutedEventArgs e)
        {
            if (!App.ViewModel.IsDataLoaded)
            {
                App.ViewModel.LoadData();
            }

            Pivot_Items.Header = MainPage.tipes[MainPage.savedOptions.type].Name;
            if (App.ViewModel.Promotions.Count == 0)
                PivotControl.Items.Remove(Pivot_Promotions);
        }

        private void Items_List_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            //ItemPage.SelectedItem = ((ItemViewModel)sender)._Item;
            var item = Items_List.SelectedItem as ItemViewModel;
            if (item == null || item._Item == null)
                return;
            ItemPage.SelectedItem = item._Item;
            Items_List.SelectedIndex = -1;
            NavigationService.Navigate(new Uri("/ItemPage.xaml", UriKind.Relative));
        }

        private void Promotions_List_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var item = Promotions_List.SelectedItem as PromotionViewModel;
            if (item == null || item._Item == null)
                return;
            PromotionPage.SelectedItem = item._Item;
            Promotions_List.SelectedIndex = -1;
            NavigationService.Navigate(new Uri("/PromotionPage.xaml", UriKind.Relative));
        }
    }
}